package coderead.maven.job.dao;

import coderead.maven.job.bean.Artifact;
import coderead.maven.job.bean.ArtifactClass;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Slf4j
@Repository
@RequiredArgsConstructor
public class ArtifactMapper {
    public static byte unIndex = 0;
    public static byte indexing = 1;
    public static byte succeed = 2;
    public static byte fail = 3;

    private final JdbcTemplate template;

    /**
     * 基于索引的构建状态 查找id
     *
     * @param startId
     * @param indexClassState
     * @param size
     * @return
     */
    public List<Artifact> findArtifactByState(Long startId,
                                              Byte indexClassState,
                                              int size) {
        startId = startId == null ? 0L : startId;
        List<Artifact> result = template.query("select * from artifact where id > ? and index_class = ? limit ?",
                new BeanPropertyRowMapper<>(Artifact.class), startId, indexClassState, size);
        return result;
    }

    /**
     * 批量更新 状态
     *
     * @param ids
     * @param indexClassState
     */
    public void setArtifactSate(Integer[] ids, Byte indexClassState) {
        template.batchUpdate("update artifact set index_class = ? where id = ?", new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setByte(1, indexClassState);
                ps.setInt(2, ids[i]);
            }

            @Override
            public int getBatchSize() {
                return ids.length;
            }
        });
    }


    /**
     *
     */
    @Transactional
    public synchronized void updateArtifactClass(List<ArtifactClass> list) {
        long begin = System.currentTimeMillis();
        // 删除指定组件下所有Class
        template.update("delete from artifact_class where artifact = ?", list.get(0).getArtifact());
        log.info("数量:{} 删除用时:{}", list.size(), System.currentTimeMillis() - begin);


        // 批量新增
        template.batchUpdate("insert into artifact_class (full_class_name, artifact, version) values ( ?, ?, ?)", new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                String fullClassName = list.get(i).getFullClassName();
                fullClassName = fullClassName.substring(0, Math.min(fullClassName.length(), 250));// 超出部分裁掉
                ps.setString(1, fullClassName);
                ps.setString(2, list.get(i).getArtifact());//
                ps.setString(3, list.get(i).getVersion());
            }

            @Override
            public int getBatchSize() {
                return list.size();
            }
        });
        log.info("数量:{} 新增用时:{}", list.size(), System.currentTimeMillis() - begin);

        // 修改状态
        template.update("update artifact set index_class = ? where artifact = ?", succeed,
                list.get(0).getArtifact());

        log.info("数量:{} 总用时:{}", list.size(), System.currentTimeMillis() - begin);
    }

}
